{
 "cells": [
  {
   "cell_type": "code",
   "execution_count": 7,
   "id": "db43d518-b39d-4aaf-9338-d64b2fba0ec8",
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/plain": [
       "'C:\\\\Users\\\\职云昊\\\\中国平安_2020上半年_K线图.html'"
      ]
     },
     "execution_count": 7,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "import pandas as pd\n",
    "from pyecharts import options as opts\n",
    "from pyecharts.charts import Kline\n",
    "\n",
    "# 读取xls文件，使用 xlrd 引擎\n",
    "df = pd.read_excel(\"C:/Users/职云昊/OneDrive/Desktop/中国平安.xls\", engine='xlrd')\n",
    "\n",
    "# 转换日期列为日期格式\n",
    "df['date'] = pd.to_datetime(df['date'])\n",
    "\n",
    "# 筛选出2020年上半年（1月1日至6月30日）的数据\n",
    "df = df[(df['date'] >= '2020-01-01') & (df['date'] <= '2020-06-30')]\n",
    "\n",
    "# 提取必要的列并按日期排序\n",
    "df = df[['date', 'open', 'high', 'low', 'close']]\n",
    "df = df.sort_values('date')\n",
    "\n",
    "# 提取K线数据格式，转换为Pyecharts需要的格式\n",
    "kline_data = df[['open', 'high', 'low', 'close']].values.tolist()\n",
    "\n",
    "# 创建K线图\n",
    "kline = (\n",
    "    Kline()\n",
    "    .add_xaxis(df['date'].dt.strftime('%Y-%m-%d').tolist())  # x轴为日期\n",
    "    .add_yaxis(\"K线图\", kline_data)  # y轴为开盘、最高、最低、收盘价格\n",
    "    .set_global_opts(\n",
    "        title_opts=opts.TitleOpts(title=\"2020年上半年中国平安股票价格 K线图\"),\n",
    "        xaxis_opts=opts.AxisOpts(type_=\"category\", boundary_gap=False),  # X轴为日期\n",
    "        yaxis_opts=opts.AxisOpts(\n",
    "            type_=\"value\",  # Y轴为股票价格\n",
    "            min_=66,  # 设置Y轴最小值为66\n",
    "            max_=84,  # 设置Y轴最大值为84\n",
    "        ),\n",
    "        datazoom_opts=opts.DataZoomOpts(),  # 允许缩放\n",
    "    )\n",
    ")\n",
    "\n",
    "# 渲染图表到HTML文件\n",
    "kline.render('中国平安_2020上半年_K线图.html')\n"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 11,
   "id": "43675400-68ce-4935-afa8-30c07710db5c",
   "metadata": {},
   "outputs": [],
   "source": [
    "import pandas as pd\n",
    "import plotly.graph_objects as go\n",
    "\n",
    "# 读取xls文件\n",
    "df = pd.read_excel(\"C:/Users/职云昊/OneDrive/Desktop/中国平安.xls\", engine='xlrd')\n",
    "\n",
    "# 转换日期列为日期格式\n",
    "df['date'] = pd.to_datetime(df['date'])\n",
    "\n",
    "# 筛选出2020年上半年（1月1日至6月30日）的数据\n",
    "df = df[(df['date'] >= '2020-01-01') & (df['date'] <= '2020-06-30')]\n",
    "\n",
    "# 提取OHLC图所需的列\n",
    "df_ohlc = df[['date', 'open', 'high', 'low', 'close']]\n",
    "\n",
    "# 绘制OHLC图\n",
    "fig = go.Figure(data=[go.Candlestick(\n",
    "    x=df_ohlc['date'],\n",
    "    open=df_ohlc['open'],\n",
    "    high=df_ohlc['high'],\n",
    "    low=df_ohlc['low'],\n",
    "    close=df_ohlc['close'],\n",
    "    name='OHLC'\n",
    ")])\n",
    "\n",
    "# 设置图表标题和布局\n",
    "fig.update_layout(\n",
    "    title=\"2020年上半年中国平安股票价格 OHLC 图\",\n",
    "    xaxis_title=\"日期\",\n",
    "    yaxis_title=\"股票价格\",\n",
    "    xaxis_rangeslider_visible=False  # 不显示范围滑块\n",
    ")\n",
    "\n",
    "# 将图表保存为HTML文件\n",
    "fig.write_html(\"中国平安_2020上半年_OHLC图.html\")\n"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 31,
   "id": "93b13869-81f7-404e-bd3e-96b4a2617d13",
   "metadata": {},
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "HTML文件已保存为: renko_chart.html\n"
     ]
    }
   ],
   "source": [
    "import pandas as pd\n",
    "import mplfinance as mpf\n",
    "import matplotlib.pyplot as plt\n",
    "from matplotlib import font_manager\n",
    "\n",
    "# 配置中文字体\n",
    "font_path = 'C:/Windows/Fonts/msyh.ttc'  # 微软雅黑字体路径（根据你的系统调整路径）\n",
    "font_prop = font_manager.FontProperties(fname=font_path)\n",
    "\n",
    "# 读取股票数据\n",
    "df = pd.read_excel(\"C:/Users/职云昊/OneDrive/Desktop/中国平安.xls\", engine='xlrd')\n",
    "\n",
    "# 确保日期列是正确的datetime格式\n",
    "df['date'] = pd.to_datetime(df['date'])\n",
    "\n",
    "# 筛选出2020年上半年（1月1日至6月30日）的数据\n",
    "df = df[(df['date'] >= '2020-01-01') & (df['date'] <= '2020-06-30')]\n",
    "\n",
    "# 设置日期为索引\n",
    "df.set_index('date', inplace=True)\n",
    "\n",
    "# 筛选所需的列：Open, High, Low, Close\n",
    "df = df[['open', 'high', 'low', 'close']]\n",
    "\n",
    "# 定义黑白相间的自定义样式\n",
    "renko_style = mpf.make_mpf_style(base_mpf_style='classic', rc={'font.family': font_prop.get_name()})\n",
    "\n",
    "# 使用自定义样式绘制 Renko 图，并设置纵坐标范围\n",
    "mpf.plot(df, type='renko', style=renko_style, title=\"2020年上半年中国平安股票价格 Renko 图\", ylabel=\"股票价格\", \n",
    "         ylim=(65.0, 87.5), savefig=\"renko_chart.png\")\n",
    "\n",
    "# 如果需要的话，将图像嵌入到HTML中\n",
    "html_code = '''\n",
    "<html>\n",
    "<head><title>Renko 图</title></head>\n",
    "<body>\n",
    "<h1>2020年上半年中国平安股票价格 Renko 图</h1>\n",
    "<img src=\"renko_chart.png\" alt=\"Renko 图\">\n",
    "</body>\n",
    "</html>\n",
    "'''\n",
    "\n",
    "with open(\"renko_chart.html\", \"w\", encoding=\"utf-8\") as f:\n",
    "    f.write(html_code)\n",
    "\n",
    "print(\"HTML文件已保存为: renko_chart.html\")\n"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 31,
   "id": "4b159c13-40ef-4f58-8e2b-6dce9aca25df",
   "metadata": {},
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "HTML文件已保存为: renko_chart.html\n"
     ]
    }
   ],
   "source": [
    "import pandas as pd\n",
    "import mplfinance as mpf\n",
    "import matplotlib.pyplot as plt\n",
    "from matplotlib import font_manager\n",
    "\n",
    "# 配置中文字体\n",
    "font_path = 'C:/Windows/Fonts/msyh.ttc'  # 微软雅黑字体路径（根据你的系统调整路径）\n",
    "font_prop = font_manager.FontProperties(fname=font_path)\n",
    "\n",
    "# 读取股票数据\n",
    "df = pd.read_excel(\"C:/Users/职云昊/OneDrive/Desktop/中国平安.xls\", engine='xlrd')\n",
    "\n",
    "# 确保日期列是正确的datetime格式\n",
    "df['date'] = pd.to_datetime(df['date'])\n",
    "\n",
    "# 筛选出2020年上半年（1月1日至6月30日）的数据\n",
    "df = df[(df['date'] >= '2020-01-01') & (df['date'] <= '2020-06-30')]\n",
    "\n",
    "# 设置日期为索引\n",
    "df.set_index('date', inplace=True)\n",
    "\n",
    "# 筛选所需的列：Open, High, Low, Close\n",
    "df = df[['open', 'high', 'low', 'close']]\n",
    "\n",
    "# 定义黑白相间的自定义样式\n",
    "renko_style = mpf.make_mpf_style(base_mpf_style='classic', rc={'font.family': font_prop.get_name()})\n",
    "\n",
    "# 使用自定义样式绘制 Renko 图，并设置纵坐标范围\n",
    "mpf.plot(df, type='renko', style=renko_style, title=\"2020年上半年中国平安股票价格 Renko 图\", ylabel=\"股票价格\", \n",
    "         ylim=(65.0, 87.5), savefig=\"renko_chart.png\")\n",
    "\n",
    "# 如果需要的话，将图像嵌入到HTML中\n",
    "html_code = '''\n",
    "<html>\n",
    "<head><title>Renko 图</title></head>\n",
    "<body>\n",
    "<h1>2020年上半年中国平安股票价格 Renko 图</h1>\n",
    "<img src=\"renko_chart.png\" alt=\"Renko 图\">\n",
    "</body>\n",
    "</html>\n",
    "'''\n",
    "\n",
    "with open(\"renko_chart.html\", \"w\", encoding=\"utf-8\") as f:\n",
    "    f.write(html_code)\n",
    "\n",
    "print(\"HTML文件已保存为: renko_chart.html\")\n"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "id": "cf64c1c8-64e0-4ae9-b784-c7d08e54437a",
   "metadata": {},
   "outputs": [],
   "source": []
  }
 ],
 "metadata": {
  "kernelspec": {
   "display_name": "Python 3 (ipykernel)",
   "language": "python",
   "name": "python3"
  },
  "language_info": {
   "codemirror_mode": {
    "name": "ipython",
    "version": 3
   },
   "file_extension": ".py",
   "mimetype": "text/x-python",
   "name": "python",
   "nbconvert_exporter": "python",
   "pygments_lexer": "ipython3",
   "version": "3.12.4"
  }
 },
 "nbformat": 4,
 "nbformat_minor": 5
}
